<template>
  <!-- 对话框 -->
  <!-- 新增部门的弹层 -->
  <!-- :model  :rules  prop  v-model -->
  <el-dialog title="新增部门" :visible="isShow" @close="handleClose">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-10个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-10个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in peoples"
            :key="item.id"
            :value="item.username"
            :label="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <div slot="footer">
      <el-button
        type="primary"
        size="small"
        @click="clickSubmit"
      >确定</el-button>
      <el-button size="small">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { reqGetEmployeeSimple, reqAddDepartments } from '@/api/departments'
export default {
  props: {
    isShow: {
      type: Boolean,
      default: true
    },
    nodeData: {
      type: Object,
      required: true
    },
    deptsList: {
      type: Array,
      required: true
    }
  },
  data() {
    const checkNameRepeat = (rule, value, callback) => {
      // 过滤得到同级所有部门
      const arr = this.deptsList.filter((item) => item.pid === this.nodeData.id)
      // 遍历同级部门, 看有没有重名的

      const flag = arr.some((item) => item.name === value)

      // console.log(flag, 'qqqqqqqqqqqqq')
      // 判断进行校验
      flag
        ? callback(new Error(`同级部门下已经有${value}的部门了`))
        : callback()
    }
    // 校验部门编码是否重复
    const checkCodeRepeat = (rule, value, callback) => {
      // 所有部门, 编码都唯一, 不管层级
      const isRepeat = this.deptsList.some((item) => item.code === value)
      isRepeat ? callback(new Error(`已经有编码${value}的部门了`)) : callback()
    }
    return {
      peoples: [],
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          {
            required: true,
            message: '部门名称不能为空',
            trigger: ['blur', 'change']
          },
          {
            min: 1,
            max: 10,
            message: '部门名称只能是1-10位',
            trigger: ['blur', 'change']
          },
          { validator: checkNameRepeat, trigger: 'blur' }
        ],
        code: [
          {
            required: true,
            message: '部门编码不能为空',
            trigger: ['blur', 'change']
          },
          {
            min: 1,
            max: 10,
            message: '部门编码只能是1-10位',
            trigger: ['blur', 'change']
          },
          { validator: checkCodeRepeat, trigger: 'blur' }
        ],
        manager: [
          {
            required: true,
            message: '部门负责人不能为空',
            trigger: ['blur', 'change']
          }
        ],
        introduce: [
          {
            required: true,
            message: '部门介绍不能为空',
            trigger: ['blur', 'change']
          },
          {
            min: 1,
            max: 300,
            message: '部门介绍只能是1-300位',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  created() {
    this.getEmployeeSimple()
  },
  methods: {
    handleClose() {
      console.log('关闭了')
      this.$emit('handleClose')
    },
    // 获取员工简单列表数据
    async getEmployeeSimple() {
      const res = await reqGetEmployeeSimple()
      this.peoples = res.data
      console.log(this.peoples, 'people')
    },
    // // 提交
    // clickSubmit() {
    //   this.$refs.deptForm.validate(async isOK => {
    //     if (isOK) {
    //       // 表示可以提交了
    //       // 调用新增接口 添加父部门的id
    //       await reqAddDepartments({ ...this.form, pid: this.nodeData.id })
    //       // 通知到父组件, 添加操作
    //       this.$emit('add-depts')
    //       // 关闭弹层
    //       this.$emit('handleClose', false)
    //     }
    //   })
    // }
    clickSubmit() {
      this.$refs.deptForm.validate(async(flag) => {
        if (!flag) return
        const res = await reqAddDepartments({
          ...this.form,
          pid: this.nodeData.id
        })
        console.log(res)
        this.$message.success('新增部门成功')
        this.handleClose()
        this.$emit('getD')
      })
    }
  }
}
</script>

<style>
</style>
